<?php
$act = isset($_GET['act']) ? trim($_GET['act']) : '0';
if($act == 'export'){
        include "./lib/db.class.php";
        $db =  new DB();
        
        //取出站点地址设置
        $c = $db->get_all('select * from '.$db->prefix.'options where name= "routingTable" or name="siteUrl"' );
        $path = unserialize($c[0]['value']);
        $path = $path['post']['url'];
        $path = $c[1]['value'].$path;

        //取出必要的内容
        $info = $db->get_all('select m.*,t.*,s.name as sname,s.slug as sslug,n.title,n.slug,n.created as ncreated from '.$db->prefix.'comments m left join '.$db->prefix.'relationships t on m.cid = t.cid left join '.$db->prefix.'metas s on t.mid=s.mid left join '.$db->prefix.'contents as n on m.cid=n.cid  where m.status = "approved" and m.type="comment" and s.type="category"');
        
        $arr = array();
        foreach($info as $k => $v){
        	$arr["threads"][$k] = array(
        		"author_key" => "1",
        		"thread_key" => $v["cid"],
        		"title" => $v["title"],
        		"url" => str_replace(
                                array('[cid:digital]','[slug]','[year:digital:4]','[month:digital:2]','[day:digital:2]','[category]'),
                                array($v["cid"],$v["slug"], date('Y',$v['created']),date('m',$v['created']),date('d',$v['created']),$v["sslug"]),
                                $path
                        ),
        	);
        	$arr["posts"][$k] = array(
        		"author_key" => "1",
        		"post_key" => $v["coid"],
                "thread_key" => $v["cid"],
                "parent_key" => $v["parent"],
                "author_name" => $v["author"],
                "author_key" => ( $v["authorId"] == 1 ) ? $db->authorId : $v["authorId"],
                "author_email" => $v["mail"],
                "author_url" => $v["url"],
                "created_at" => date("Y-m-d H:i:s" , $v["created"]),
                "agent" => $v["agent"],
                "ip" => $v["ip"],
                "message" => preg_replace('/<(.*?)>/i', '', $v["text"]),
                "status" => "approved",
        	);

        }
        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=duoshuo.json");
        echo json_encode($arr);
}else{?>
<!doctype html>
<html lang="en">
<head>
        <meta charset="UTF-8">
        <title>Typecho to Duoshuo</title>
        <style>
            a{text-decoration: none;color: #666}
            .warp{width:500px;margin:20px auto;border:#ccc dotted 1px;padding:10px;}
            .tip{line-height: 24px;}
            .tip p{border-bottom: #ccc dotted 1px;padding-bottom: 5px}
            .tip .copy{display: block;text-align: right;margin-top:10px;}
            .down{margin-top: 5px;border-top: #ccc dotted 1px;padding-top: 10px;}
            .down a{text-decoration: none;color:#f00;border:#ccc solid 1px;padding:3px;}
        </style>
</head>
<body>
    <div class="warp">
        <div class="tip">
            <p>说明：一键导出评论到多说...</p>
            <span class="notice">
                使用方法：<br/>
                1.打开config.inc.php 输入必要参数,全部都必要;<br/>
                2.导出的文件去多说导入即可<br/>
                3.如果评论及文章数量很大，请于本地执行<br/>
            </span>
            <span class="copy">
                by 阳光 [<a href="http://ysido.com">http://ysido.com</a>]<br/>2013.10.23
            </span>
        </div>
        <div class="down">执行操作：<a href="?act=export">导出评论</a></div>
    </div>
    </body>
</html>
<?php }?>
